<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="large" :inline="true">
      <el-form-item label="会员名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入会员名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="telName">
        <el-input
          v-model="queryParams.telName"
          placeholder="请输入联系人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机" prop="telNumber">
        <el-input
          v-model="queryParams.telNumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="large"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="large" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="18">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="large"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="large"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <el-table
      class="address-table"
      v-loading="loading"
      :data="addressList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地址编号" prop="id" width="120" align="center" />
      <el-table-column
        label="会员名称"
        prop="username"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
      />
      <el-table-column label="联系人" prop="telName" width="120" align="center">
      </el-table-column>
      <el-table-column
        label="手机"
        prop="telNumber"
        width="200"
        align="center"
      />
      <el-table-column
        label="邮政编码"
        prop="postalCode"
        width="200"
        align="center"
      />
      <el-table-column
        label="省市区"
        prop="fullLocation"
        width="200"
        align="center"
      />
      <el-table-column
        label="详细地址"
        prop="detail"
        width="200"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="large"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-row :span="24" style="margin-top: 20px">
      <el-col :span="24" :offset="15">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNo"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryParams.pageSize"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
        >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "AddressComponent",
  data() {
    return {
      totalCount: 0,
      //搜索参数
      queryParams: {
        username: "",
        telName: "",
        telNumber:"",
        pageNo: 1,
        pageSize: 5,
      },
      //地址列表
      addressList: [],
      // 非多个禁用
      multiple: true,
      loading: false,
      showSearch: true,
      ids: [], //选中的购物车id集合
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取地址列表
    async getList() {
      this.loading = true;
      try {
        const res = await this.$axios("/api/address/list", {
          params: this.queryParams,
        });
        if (res.data.code === 200) {
          this.totalCount = res.data.data.totalCount;
          this.addressList  = res.data.data.data;
        }
      } catch (error) {
        this.$message.error("获取地址列表失败");
      } finally {
        this.loading = false;
      }
    },
    //查询
    handleQuery() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.getList();
        } else {
          return false;
        }
      });
    },
    //重置查询条件
    resetQuery() {
      this.queryParams.username = "";
      this.queryParams.telName = "";
      this.queryParams.telNumber = "";
      this.getList();
    },
    //当页面变化时的事件
    handleCurrentChange(pageNo) {
      //修改当前页码
      this.queryParams.pageNo = pageNo;
      //执行查询
      this.handleQuery();
    },

    //每当页数量下拉框选项切换变化时的事件
    handleSizeChange(pageSize) {
      //修改查询条件中的的每页数量
      this.queryParams.pageSize = pageSize;
      //执行查询
      this.handleQuery();
    },
    handleDelete(row) {
      const id = [row.id ]|| this.ids;
      this.$confirm(`是否删除地址编号为${id}的地址`, "提示", {
        type: "warning",
      })
        .then(async () => {
          const res = await this.$axios.post(
            "/api/address/del",
            id
          );
          if (res.data.code === 200) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    handleExport() {
      // 导出逻辑
      this.$message.info("暂不支持导出功能");
    },
    //多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
  },
};
</script>

<style scoped>
.address-table {
  margin-top: 20px;
}
.app-container {
  padding: 20px;
}
.el-form-item {
  margin-bottom: 20px;
}
.el-form-item label {
  font-size: 16px;
  font-weight: bold;
}
</style>
